package com.atguigu.spzx.controller;

import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.service.SysUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "用户模块")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    @PostMapping("/findPage/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询")
    public Result<PageInfo<SysUser>> findPage(@RequestBody SysUserDto sysUserDto,
                                              @PathVariable(value = "pageNum") Integer pageNum,
                                              @PathVariable(value = "pageSize") Integer pageSize) {


        PageInfo<SysUser> pageInfo = sysUserService.findPage(sysUserDto, pageNum, pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);


    }

    @PostMapping("/saveUser")
    @Operation(summary = "添加")
    public Result saveUser(@RequestBody SysUser sysUser) {
        if (sysUserService.findByUsername(sysUser) != null) {
            return Result.build(null, ResultCodeEnum.USER_NAME_IS_EXISTS);
        }
        sysUserService.saveUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/updateUser")
    @Operation(summary = "修改")
    public Result updateUser(@RequestBody SysUser sysUser) {
        sysUserService.updateUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/removeById/{id}")
    @Operation(summary = "删除")
    public Result removeById(@PathVariable Long id) {
        sysUserService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
